package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import bean.comissaoBean;

public class comissaoDao {

	public void inserir(Connection conn, comissaoBean comissao) throws SQLException {

		String sql = "insert into comissao(idfuncionario, idOS) values(?, ?)";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, comissao.getIdFuncionario());
		ps.setInt(2, comissao.getIdOS());
	
		ps.execute();
		ps.close();
	}

	public void alterar(Connection conn, comissaoBean comissao) throws SQLException {

		String sql = "update comissao set idfuncionario = ?, idOS = ?, valor = ?, pagou = ? where id = ?";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, comissao.getIdFuncionario());
		ps.setInt(2, comissao.getIdOS());
		ps.setDouble(3, comissao.getValor());
		ps.setInt(4, comissao.getPagou());
		ps.setInt(5, comissao.getId());
		
		ps.execute();
		ps.close();
	}

	public void remover(Connection conn, comissaoBean comissao) throws SQLException {

		String sql = "delete from comissao where id = ?";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, comissao.getId());
		ps.execute();
		ps.close();
	}

	public ArrayList<comissaoBean> getLista(Connection conn) throws SQLException {

		String sql = "select * from comissao";

		PreparedStatement ps = conn.prepareStatement(sql);

		ResultSet rs = ps.executeQuery();
		ArrayList<comissaoBean> comissaos = new ArrayList<comissaoBean>();
		while (rs.next()) {
			comissaoBean comissao = new comissaoBean();
			comissao.setId(rs.getInt("id"));
			comissao.setIdFuncionario(rs.getInt("idfuncionario"));
			comissao.setIdOS(rs.getInt("idos"));
			comissao.setValor(rs.getDouble("valor"));
			comissao.setPagou(rs.getInt("pagou"));
			
			comissaos.add(comissao);
		}
		ps.close();
		rs.close();

		return comissaos;

	}

	public comissaoBean getcomissao(Connection conn, comissaoBean comissaoBean)
			throws SQLException {

		String sql = "select * where id = ?";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, comissaoBean.getId());

		ResultSet rs = ps.executeQuery();

		comissaoBean comissao = new comissaoBean();
		comissao.setId(rs.getInt("id"));
		comissao.setIdFuncionario(rs.getInt("idfuncionario"));
		comissao.setIdOS(rs.getInt("idos"));
		comissao.setValor(rs.getDouble("valor"));
		comissao.setPagou(rs.getInt("pagou"));
		
		ps.close();
		rs.close();

		return comissao;

	}

	public void alterarDesigner(Connection conn, comissaoBean comissao) throws SQLException {
		
		String sql = "update comissao set idfuncionario = ? where idOS = ?";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, comissao.getIdFuncionario());
		ps.setInt(2, comissao.getIdOS());
		
		ps.execute();
		ps.close();
		
	}

}
